home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / slock / dlltest1.pas < prev    next >
Pascal/Delphi Source File  |  1998-08-17  |  9KB  |  260 lines

  1. unit dlltest1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, SlokUtil;
  8.  
  9. type
  10.   TfrmBufferTools = class(TForm)
  11.     grpBuffer: TGroupBox;
  12.     mmoBuffer: TMemo;
  13.     btnShowBuffer: TButton;
  14.     grpFindBuffer: TGroupBox;
  15.     btnFindBuffer: TButton;
  16.     lblFindExpl: TLabel;
  17.     lblOffsetLabel: TLabel;
  18.     lblOffsetData: TLabel;
  19.     grpWriteBuffer: TGroupBox;
  20.     btnWriteBuffer: TButton;
  21.     lblWriteExpl: TLabel;
  22.     grpDLLName: TGroupBox;
  23.     edtDLLName: TEdit;
  24.     lblDLLExpl: TLabel;
  25.     procedure btnShowBufferClick(Sender: TObject);
  26.     procedure btnFindBufferClick(Sender: TObject);
  27.     procedure btnWriteBufferClick(Sender: TObject);
  28.   end;
  29.  
  30. var
  31.   frmBufferTools: TfrmBufferTools;
  32.  
  33. implementation
  34.  
  35. {$R *.DFM}
  36.  
  37. {*******************************************************************************
  38. * Procedure : btnShowBufferClick                                               *
  39. ********************************************************************************
  40. * Purpose   : Shows the contents of the buffer in the DLL                      *
  41. ********************************************************************************
  42. * Paramters : None                                                             *
  43. ********************************************************************************
  44. * Returns   : None                                                             *
  45. *******************************************************************************}
  46. procedure TfrmBufferTools.btnShowBufferClick(Sender: TObject);
  47. type
  48.      TGetRegInfo = function: TBuffer; stdcall;
  49. var
  50.      GetRegInfo: TGetRegInfo;
  51.      MyHandle: THandle;
  52.      Buffer: TBuffer;
  53.      BufferString: string;
  54.      i: integer;
  55.      DLLNameHelper: PChar;
  56. begin
  57.      // clear the memo
  58.      mmoBuffer.Clear;
  59.  
  60.      // get the name of the DLL in the right (PChar) form
  61.      DLLNameHelper := AllocMem(255);
  62.      StrPCopy(DLLNameHelper, edtDLLName.Text);
  63.  
  64.      // get the handle of the DLL and attempt to load
  65.      MyHandle := LoadLibrary(DLLNameHelper);
  66.      if MyHandle <> 0 then
  67.      begin
  68.           // if the load was successful, get the address of the GetRegInfo function
  69.           @GetRegInfo := GetProcAddress(MyHandle, 'GetRegInfo');
  70.  
  71.           // if the address is valid
  72.           if @GetRegInfo <> nil then
  73.           begin
  74.                // get the buffer
  75.                Buffer := GetRegInfo;
  76.  
  77.                // output the buffer
  78.                for i := 0 to 255 do
  79.                begin
  80.                     BufferString := BufferString + IntToHex(Buffer[i],2) + ' ';
  81.                     if ((i mod 16) = 15) then
  82.                     begin
  83.                          mmoBuffer.Lines.Add(BufferString);
  84.                          BufferString := '';
  85.                     end;
  86.                end;
  87.           end
  88.           else
  89.           begin
  90.                mmoBuffer.Lines.Add('Error reading buffer');
  91.           end;
  92.  
  93.      // tidy up
  94.      FreeLibrary(MyHandle);
  95.      FreeMem(DLLNameHelper);
  96.      end;
  97. end;
  98.  
  99. {*******************************************************************************
  100. * Procedure : btnFindBufferClick                                               *
  101. ********************************************************************************
  102. * Purpose   : Finds the start position (offset) of the buffer in the DLL       *
  103. ********************************************************************************
  104. * Paramters : None                                                             *
  105. ********************************************************************************
  106. * Returns   : None                                                             *
  107. *******************************************************************************}
  108. procedure TfrmBufferTools.btnFindBufferClick(Sender: TObject);
  109. var
  110.      OutputFile: File of Byte;
  111.      b,c,d: Byte;
  112.      Counter: integer;
  113. begin
  114.      // check if the DLL is in the current directory
  115.      if not FileExists(edtDLLName.Text) then
  116.      begin
  117.           // the DLL was not found
  118.           ShowMessage('The DLL was not found - it must be in the same directory as this project to test it!');
  119.           Exit;
  120.      end; {if}
  121.  
  122.      // tell us what's going on
  123.      lblOffsetData.Caption := 'Searching... (will take a moment)';
  124.      lblOffsetData.Repaint;
  125.  
  126.      try
  127.           AssignFile(OutputFile, edtDLLName.Text);
  128.      except
  129.           // we could not open the output file for some reason
  130.           // so exit gracefully with an error code
  131.           CloseFile(OutputFile);
  132.           Exit;
  133.      end;
  134.  
  135.      // reset the output file
  136.      FileMode := 2;
  137.      Reset(OutputFile);
  138.  
  139.      Counter := -1;
  140.  
  141.      // locate the right place in the file
  142.      while not EOF(OutputFile) do
  143.      begin
  144.           Inc(Counter);
  145.           Read(OutputFile,b);
  146.           if b = Ord('B') then
  147.           begin
  148.                Read(OutputFile,b,c,d);
  149.                if ((b = ORD('u')) and (c = Ord('f')) and (d = ORD(':'))) then
  150.                begin
  151.                     // display the offset of the buffer from file start
  152.                     lblOffsetData.Caption := IntToStr(Counter + 4);
  153.                     CloseFile(OutputFile);
  154.                     Exit;
  155.                end
  156.                else
  157.                begin
  158.                     Inc(Counter,3);
  159.                end;
  160.           end;
  161.      end;
  162.      lblOffsetData.Caption := 'Not Found!!';
  163.      CloseFile(OutputFile);
  164. end;
  165.  
  166. {*******************************************************************************
  167. * Procedure : btnWriteBufferClick                                              *
  168. ********************************************************************************
  169. * Purpose   : Writes into the buffer                                           *
  170. ********************************************************************************
  171. * Paramters : None                                                             *
  172. ********************************************************************************
  173. * Returns   : None                                                             *
  174. *******************************************************************************}
  175. procedure TfrmBufferTools.btnWriteBufferClick(Sender: TObject);
  176. type
  177.      TGetBuffAddr = function(DLLName: PChar): integer; stdcall;
  178. var
  179.      GetBuffAddr: TGetBuffAddr;
  180.      MyHandle: THandle;
  181.      DLLNameHelper: PChar;
  182.      OutputFile: File of Byte;
  183.      b: Byte;
  184.      i: integer;
  185.      BuffOffset: Integer;
  186. begin
  187.      // check if the DLL is in the current directory
  188.      if not FileExists(edtDLLName.Text) then
  189.      begin
  190.           // the DLL was not found
  191.           ShowMessage('The DLL was not found - it must be in the same directory as this project to test it!');
  192.           Exit;
  193.      end; {if}
  194.  
  195.      // initialise
  196.      BuffOffset := 0;
  197.  
  198.      // get the name of the DLL in the right (PChar) form
  199.      DLLNameHelper := AllocMem(255);
  200.      StrPCopy(DLLNameHelper, edtDLLName.Text);
  201.  
  202.      // get the handle to the DLL and load it
  203.      MyHandle := LoadLibrary(DLLNameHelper);
  204.      if MyHandle <> 0 then
  205.      begin
  206.           // if we were successful, get the address of the function
  207.           @GetBuffAddr := GetProcAddress(MyHandle, 'GetBuffAddr');
  208.           if @GetBuffAddr <> nil then
  209.           begin
  210.                // get the offset from the DLL
  211.                BuffOffset := GetBuffAddr(DLLNameHelper);
  212.           end;
  213.      end
  214.      else
  215.      begin
  216.          // no dll found - tell the user and exit
  217.          ShowMessage('DLL not found!');
  218.          FreeLibrary(MyHandle);
  219.          FreeMem(DLLNameHelper);
  220.          Exit;
  221.      end;
  222.  
  223.      // update the display
  224.      lblOffsetData.Caption := IntToStr(BuffOffset);
  225.  
  226.      // tidy up
  227.      FreeLibrary(MyHandle);
  228.      FreeMem(DLLNameHelper);
  229.  
  230.      // write into the DLL
  231.      try
  232.           AssignFile(OutputFile, edtDLLName.Text);
  233.      except
  234.           // we could not open the output file for some reason
  235.           // so exit gracefully with an error code
  236.           CloseFile(Outpu